import { getTempInfpByID} from '@/api/proTemplate.js';
const templMixin = {
    data() {
        return {
            defImg: require('../../../assets/home/defImg.png'),
            picObj:{}
        }
    },
    methods: {
        getTempInfpByID(id){
            return new Promise((resolve,reject) => {
                if(this.picObj[id]){
                    resolve(this.picObj[id])
                    return
                }
                getTempInfpByID(id)
                    .then(({ data:{code, data, message} }) => {
                        if (code === 1) {
                            if(data.picUrl){
                                data.picUrl = `/files${data.picUrl}`
                            }else{
                                data.picUrl = this.defImg
                            }
                            this.picObj[data.id] = data;
                            resolve(data)
                        }else{
                            data.picUrl = this.defImg
                            reject({})
                        }
                    })
                    .catch((err) => {
                        reject({})
                        console.log(err);
                });
            })
        },
        setDefImg(event, defImg) {
            let img = event.srcElement;
            img.src = defImg || this.defImg;
            img.onerror = null;
        }
    }
};
export { templMixin };
